﻿using OA.BLL;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;

using Z.EntityFramework.Plus;
using Microsoft.Extensions.Caching.Memory;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using OA.Web.Attribute;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;

namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class RightsManagementController : BaseMemberController
    {
        private IMemoryCache _cache;
        public RightsManagementController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("206991", "权限管理", "管理中心", true, "iconquanxian2")]
        public IActionResult Index(string keyword, int page = 1, int limit = 20)
        {
            return View();
        }

        [MyActionFilter("206991", "权限管理")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 15)
        {
            
            Expression<Func<PermissionGroup, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m =>m.Name.Contains(keyword);
            }
            var list = this.dbContextFactory.DbContext.PermissionGroup.Where(where).Select(SimplePermissionGroup.Map).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }


        [HttpGet]
        [MyActionFilter("206991001", "添加权限组", "权限管理", false, "")]
        public IActionResult Create()
        {
            ViewBag.Menus = GetMenuInfos();
            return View(new PermissionGroup() { });
        }




        [HttpPost]
        [MyActionFilter("206991001", "添加权限组", "权限管理", false, "")]
        public IActionResult Create(string name,List<string> codes)
        {
            BaseResult baseResult = new BaseResult();
            if (string.IsNullOrWhiteSpace(name))
            {
                baseResult.State = 400;
                baseResult.Msg = "权限组名称不能为空";
                return Json(baseResult);
            }
            if (codes == null)
            {
                codes = new List<string>();
            }
            PermissionGroup permissionGroup = new PermissionGroup();
            permissionGroup.CreateDateTime = DateTime.Now;
            permissionGroup.Name = name;
            permissionGroup.Status = 1;
            permissionGroup.PermissionContext = JsonConvert.SerializeObject(codes);
            this.AddInfo<PermissionGroup>(permissionGroup);
            baseResult.Msg = "权限组添加成功";
            return Json(baseResult);

        }

        [HttpGet]
        [MyActionFilter("206991002", "编辑权限", "权限管理", false, "")]
        public IActionResult Edit(int id)
        {
            PermissionGroup infoPermissionGroup = this.GetInfo<PermissionGroup>(id);
            if (infoPermissionGroup == null)
            {
                return Redirect("/Management/RightsManagement/Index?msg=沒有找到该权限组");
            }
            return View(infoPermissionGroup);
        }

        [HttpPost]
        [MyActionFilter("206991002", "编辑权限", "权限管理", false, "")]
        public IActionResult Edit(int id, PermissionGroup model, List<string> codes)
        {
            BaseResult baseResult = new BaseResult();
            string permissionContext = JsonConvert.SerializeObject(codes);
            this.dbContextFactory.DbContext.PermissionGroup.Where(m => m.ID == id).Update(m=>new PermissionGroup() {Name=model.Name, PermissionContext= permissionContext });
            baseResult.Msg = "权限修改成功";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilter("206991003", "删除权限组", "权限管理", false, "")]
        public IActionResult Delete(int id)
        {
            Expression<Func<PermissionGroup, bool>> where = m => m.ID == id;
            this.Delete<PermissionGroup>(where);
            return Json(new { State = 200, Msg = "权限组删除成功" });
        }
    }
}